// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var constant = require("constant");
	var leftMenu = require("../leftMenu/leftMenu");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./detainedApplicationDetail.css");
	var tools = require("tools");
	var detainedId;
	var html_dispose_dentanied = require("./html_dispose_detained_application.html");
	var js_platform_main = require("./platform_main.js");
	var init = function(page) {
		detainedId = page.params.detainedId;
		inquireDetainedApplicationDetail();
//		leftMenu.init(6);
//		js_platform_main.init(0);
		$(".left").removeClass("none");
//		$("#leftMenu7").find(".leftMenu-item").removeClass("leftMenu-active");
//		$("#leftMenu7 .leftMenu li:last-child").addClass("leftMenu-open");
//		$("#leftMenu7 .leftMenu-disable").removeClass("leftMenu-open");
//		$("#leftMenu7 .leftMenu-disable:last-child").addClass("leftMenu-open");
		
		initEvent();
		router.init();

	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//同意提货申请
		$("#consentPrePickup").on("click", function() {
			router.route("main/pickupPreConsent.html?pickupId=" + pickupId);
		});

		$("#disposeDetainedApplicationBtn2").unbind("click").click(function() {
			$("#popdialog").html(html_dispose_dentanied);
			dialog.showDialog($("#detained_application_dialog"));
			$("#disposeDetainedDetainedId").val(detainedId);
			$("#detained_application_dialog .sure_yes").unbind("click").click(function() {
				$("#detained_application_dialog").attr("action", "consentDetainedApplication");
				databind.submitDom($("#detained_application_dialog"), function() {
					dialog.hideDialog($("#detained_application_dialog"));
					base.alert(1, "同意扣货申请成功！");
					router.route("main/detainedList.html?left=13", true);
				});
			});

			$("#detained_application_dialog .sure_no").unbind("click").click(function() {
				dialog.hideDialog($("#detained_application_dialog"));
			});
		});

		$("#disposeDetainedApplicationBtn1").unbind("click").click(function() {
			$("#popdialog").html(html_dispose_dentanied);
			dialog.showDialog($("#detained_application_dialog"));
			$("#disposeDetainedDetainedId").val(detainedId);
			$("#detained_application_dialog .dialog_content").html('<div>拒绝原因：<select name="disagreeReson" class="disagreeReson"></select></div>' + $("#detained_application_dialog .dialog_content").html());
			tools.inquireCommonSelectorList(9, $(".disagreeReson"));

			$("#detained_application_dialog .sure_yes").unbind("click").click(function() {
				$("#detained_application_dialog").attr("action", "opposeDetainedApplication");
				databind.submitDom($("#detained_application_dialog"), function() {
					dialog.hideDialog($("#detained_application_dialog"));
					base.alert(1, "拒绝扣货申请成功！");
					router.route("main/detainedList.html?left=13", true);
				});
			});

			$("#detained_application_dialog .sure_no").unbind("click").click(function() {
				dialog.hideDialog($("#detained_application_dialog"));
			});
		});
	};

	//提前还款详情
	var inquireDetainedApplicationDetail = function() {
		base.fnAjax({
			action: "inquireDetainedApplicationDetail.json",
			oData: {
				detainedId: detainedId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#detainedApplicationDetail"), data.datas[0]);
					juicer.register("getValueByKey", constant.getValueByKey);

					var tpl = $("#detainedApplicationTpl").html();
					var html = juicer(tpl, data);
					$("#detainedApplicationBody").html(html);
					tools.inquireCommonSelectorList(9, $(".delayReson"));
					databind.disableInput($("#detainedApplicationDetail"));

//					//物流追踪
//					$(".logisticsTrack").unbind("click").click(function() {
//						base.alert(2, "正在开发中");
//						return;
//					});

					//详情绑定事件
					$(".detail").unbind("click").click(function() {
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});

					//异常绑定事件

					$(".abnormal").unbind("click").click(function() {
						router.route("bulkstockTallyDetail/bulkstockAbnormalDetail.html?inventoryId=" + $(this).parents("tr").attr("inventoryId"));
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});